#!/bin/bash

sudo apt update
sudo apt upgrade

sudo apt-get install git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0

sudo apt install minicom


if [ ! -d ~/esp ];then
    mkdir -p ~/esp
fi

pushd ~/esp

    STABLE_TAG="v4.3"

    echo "请人工确认当前稳定版版本号，输入回车继续。"

    xdg-open https://docs.espressif.com/projects/esp-idf/zh_CN/stable/esp32/  &

    git clone --recursive -b $STABLE_TAG https://github.com/espressif/esp-idf.git

    # git clone -b v4.3 --recursive https://github.com/espressif/esp-idf.git esp-idf-v4.3
    # cd esp-idf-v4.3/

    pushd ./esp-idf
        export IDF_GITHUB_ASSETS="dl.espressif.com/github_assets"

        sudo chmod a+x install.sh
        ./install.sh

        echo "alias get_idf='. $HOME/esp/esp-idf/export.sh'" >> ~/.bashrc

        echo "请人工运行source ~/.bashrc，输入回车继续。"
    popd

popd

# https://github.com/espressif/esp-idf/commit/38c148b904670897c4b4ecd29202d6acfaa335f8


# Traceback (most recent call last):
#   File "/home/cm102/esp/esp-idf/tools/idf.py", line 812, in <module>
#     main()
#   File "/home/cm102/esp/esp-idf/tools/idf.py", line 730, in main
#     cli(sys.argv[1:], prog_name=PROG, complete_var='_IDF.PY_COMPLETE')
#   File "/home/cm102/.espressif/python_env/idf4.3_py3.8_env/lib/python3.8/site-packages/click/core.py", line 1137, in __call__
#     return self.main(*args, **kwargs)
#   File "/home/cm102/.espressif/python_env/idf4.3_py3.8_env/lib/python3.8/site-packages/click/core.py", line 1057, in main
#     self._main_shell_completion(extra, prog_name, complete_var)
#   File "/home/cm102/.espressif/python_env/idf4.3_py3.8_env/lib/python3.8/site-packages/click/core.py", line 1132, in _main_shell_completion
#     rv = shell_complete(self, ctx_args, prog_name, complete_var, instruction)
#   File "/home/cm102/.espressif/python_env/idf4.3_py3.8_env/lib/python3.8/site-packages/click/shell_completion.py", line 45, in shell_complete
#     echo(comp.source())
#   File "/home/cm102/.espressif/python_env/idf4.3_py3.8_env/lib/python3.8/site-packages/click/shell_completion.py", line 324, in source
#     self._check_version()
#   File "/home/cm102/.espressif/python_env/idf4.3_py3.8_env/lib/python3.8/site-packages/click/shell_completion.py", line 319, in _check_version
#     raise RuntimeError(
# RuntimeError: Couldn't detect Bash version, shell completion is not supported.



# cd ~/esp/hello_world
# idf.py set-target esp32
# idf.py menuconfig
# idf.py build
# idf.py -p /dev/ttyUSB0 flash monitor
# Ctrl+]

# idf.py -Dxxxx build